package yunwo.cn.yzstation.printer;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;



import java.util.ArrayList;
import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.bean.printBean.PrintLabelBean;
import yunwo.cn.yzstation.bean.printBean.PrintTicketBean;
import yunwo.cn.yzstation.utils.MoneyUtils;
import yunwo.cn.yzstation.view.activity.MainActivity;
import zpSDK.zpSDK.zpSDK;

/**
 * Created by gpayne on 2017/8/4.
 */

public class YHBPrintService extends Service {
    private boolean isPrint = false;
    public  static long lastTime = 0;
    public static String Action_print_status = "action_print_status";
    public static String Send_key_printing = "send_key_printing";
    public static String timeout = "";
    public static String Start_address_label = "Start_address_label";
    public static String Start_address_ticket = "Start_address_ticket";

    public static String Start_bean_label = "Start_bean_label";
    public static String Start_bean_ticket = "Start_bean_ticket";

    String s = "云货宝打印服务";
    private BroadcastReceiver mReceiver;
    private YH423PrintUtils mInstance;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notifi();
    }



    private void notifi() {
        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
    /* Method 01
     * this method must SET SMALLICON!
     * otherwise it can't do what we want in Android 4.4 KitKat,
     * it can only show the application info page which contains the 'Force Close' button.*/
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(YHBPrintService.this)
                .setSmallIcon(R.drawable.newlogo)
                .setTicker("云货宝")
                .setWhen(System.currentTimeMillis())
                .setContentTitle(getString(R.string.app_name))
                .setContentIntent(pendingIntent);
        Notification notification = mNotifyBuilder.build();

        startForeground(1,notification);
    }

    private void sendPrintStatus(){
        Intent sendIntent = new Intent();
        Log.e("发出的广播", String.valueOf(isPrint));
        sendIntent.setAction(Action_print_status);
        sendIntent.putExtra(Send_key_printing,isPrint);
        sendIntent.putExtra("timeout", timeout);
        getBaseContext().sendBroadcast(sendIntent);
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {

        mInstance = YH423PrintUtils.getInstance(getApplicationContext());
        while (mInstance.zpSDK.isConnected()) {
            try {
                Thread.sleep(200);
                mInstance.zpSDK.disconnect();
            } catch (InterruptedException e) {
                e.printStackTrace();
                Log.d("bbnm", e.toString());
            }
        }

       /* if (isPrint) return START_REDELIVER_INTENT;

       *//* final String labelAddress = intent.getStringExtra(Start_address_label);
        final String ticketAddress = intent.getStringExtra(Start_address_ticket);*/
//        Bundle extras = intent.getExtras();
        if (intent == null || intent.getExtras() == null) {
            return START_REDELIVER_INTENT;
        }


//        notifi();
        final PrintLabelBean labelBean = (PrintLabelBean) intent.getExtras().getSerializable(Start_bean_label);
        final PrintTicketBean ticketBean = (PrintTicketBean) intent.getExtras().getSerializable(Start_bean_ticket);
        final String ticketAddress = intent.getExtras().getString("Start_address_ticket", "");
        final String labelAddress = intent.getExtras().getString("Start_address_label", "");
        final String mPrintername = intent.getStringExtra("printername");

        Log.e("address", "小票地址"+ticketAddress+"标签地址" + labelAddress);
//        Log.e("start-->",labelAddress+"\n"+ticketAddress+"\n"+labelBean.getCompanyName()+"\n"+ticketBean.getCompanyName());
        new Thread(new Runnable() {
            @Override
            public void run() {
                isPrint = true;
                 timeout = "";
                lastTime = System.currentTimeMillis();
                sendPrintStatus();
                try {
                    print(labelAddress, labelBean, ticketAddress, ticketBean,mPrintername);

                } catch (Exception e) {
                    Log.e("kkkkk", e.toString());
                    e.printStackTrace();
                }
                isPrint = false;
                sendPrintStatus();
            }
        }).start();
//

        return super.onStartCommand(intent, flags, startId);
    }

    private void print(String labelAddress, PrintLabelBean labelBean, String ticketAddress, PrintTicketBean ticketBean, String mPrintername) {




        /*打印小票+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++开始*/
        if (ticketAddress != null && ticketAddress.length() > 0 && ticketBean != null){
            Log.d("vvvvvv", "小票:::" + ticketAddress);
            if (mPrintername.equals("XT432")) {
                for (int i = 0; i < Integer.valueOf(ticketBean.getTicket()); i++) {
                    mInstance.printTicket(ticketAddress,ticketBean);
                }
            }else {
                long lastTime = System.currentTimeMillis();
                int cout = 0;
                Log.d("cccccccccccccc", lastTime+"开始打印");
                while (true) {
                    Log.d("cccccccccccccc", "11111111111111111111"+"错误：：："+ zpSDK.ErrorMessage);

                    if (zpSDK.OpenPrinter(ticketAddress)) {
                        Log.d("cccccccccccccc", "22222222222222222连接打印机");
                        if (zpSDK.zp_page_create(68, 160-otherHeight(ticketBean))) {
                            Log.d("cccccccccccccc", "3333333333333333333 创建页面");
                            for (int i = 0; i < Integer.valueOf(ticketBean.getTicket()); i++) {
                                Log.d("cccccccccccccc", "444444444444444444 循环票数");
                                drawTicket(ticketBean);
                                zpSDK.zp_page_print(false);
                                zpSDK.zp_printer_status_detect();
                                Log.d("cccccccccccccc", "444444444444444444 循环票数结束");
                            }
                            zpSDK.zp_close();
                            break;
                        }else {
                            Log.d("cccccccccccccc", "55555555555555创建页面失败");
                            long currentTime = System.currentTimeMillis();
                            Log.d("cccccccccccccc", currentTime+"失败后打印时间"+"错误：：："+ zpSDK.ErrorMessage);

                            if (currentTime - lastTime > 20000){
                                isPrint = false;
                                sendPrintStatus();
                                break;
                            }
                        }
                    }else {

                        cout += 1;
                        Log.d("cccccccccccccc",cout+":::cout"+ "6666666666连接打印机失败:"+ticketAddress+"错误"+ zpSDK.ErrorMessage);
                        if (cout > 4) {
                            timeout = "timeout";

                            break;
                        }

                    }

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
/*打印小票+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++结束*/


        while (mInstance.zpSDK.isConnected()) {
            try {
                mInstance.zpSDK.disconnect();
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /*打印标签+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++k开始 */
        if (labelAddress != null && labelAddress.length() > 0 && labelBean != null){


            if (mPrintername.equals("XT432")) {
                for (int i = (Integer.valueOf(labelBean.getPosition())-1); i < Integer.valueOf(labelBean.getNumber()); i++) {
                    Log.d("vvvvvv", "ggggggg");
                    mInstance.printLabel(labelAddress,labelBean,i+1);
                }
            }else {
                Log.d("zzzzzz", "标签:::" + labelAddress);
                int cout = 0;
                long lastTime = System.currentTimeMillis();
                while (true){

                    if(zpSDK.OpenPrinter(labelAddress))
                    {
                        Log.d("cccccccccccccc","888888888888888");
                        if (labelBean.getNumber() == null && labelBean.getNumber().length() == 0){
                            labelBean.setNumber("1");
                        }
                        for (int i = (Integer.valueOf(labelBean.getPosition())-1); i < Integer.valueOf(labelBean.getNumber()); i++) {
                            if(zpSDK.zp_page_create(70,50)) {
                                drawLabel(labelBean,i+1);
                                zpSDK.zp_page_print(false);
                                zpSDK.zp_printer_status_detect();
                                zpSDK.zp_goto_mark_label(150);
                            }
                        }
                        zpSDK.zp_close();
                        break;
                    }else {
                        Log.d("cccccccccccccc","999999999打印机连接失败"+labelAddress+"错误："+ zpSDK.ErrorMessage);

                        Log.d("cccccccccccccc", System.currentTimeMillis()+"失败后打印时间：：标签");
                   /* if (currentTime - lastTime > 15000){
                        Log.d("cccccccccccccc","打印失败超时"+labelAddress);
                        break;
                    }*/
                        cout += 1;
                        if (cout > 4) {
                            timeout = "timeout";
                            break;
                        }
                    }
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }


    }
    private void drawTicket(PrintTicketBean ticketBean){

        zpSDK.zp_page_clear();
        Log.d("ccccccc11111", "drawTicket11111111");
        double total = 0;
        double width = 68;

        total += 3;


        zpSDK.zp_draw_text_ex(2,total+8,
                isEmpty(ticketBean.getGroupName(),"云企集团"),
                "黑体",7,0,true,false,false);
        zpSDK.zp_draw_text_ex(8,total+13,
                "-" + isEmpty(ticketBean.getCompanyName(),"")+"-",
                "黑体",3,0,false,false,false);

//        zpSDK.zp_draw_bitmap(BitmapFactory.decodeResource(getResources(),R.drawable.dan),width*10*0.65-40,total*10+5);
        zpSDK.zp_draw_text_ex(width*0.6+1,total+12,"运单号："+isEmpty(ticketBean.getTrackNumber(),""),"黑体",2.5,0,false,false,false);
        if (isEmpty(ticketBean.getSelfCodeNumber(),"").length() > 0) {
            zpSDK.zp_draw_text_ex(width * 0.6 + 1, total + 15, "自编号：" + isEmpty(ticketBean.getSelfCodeNumber(), ""), "黑体", 2.5, 0, false, false, false);
        }
        total += 15.5;
        zpSDK.zp_draw_line(0,total,width,total,1);
        String left = isEmpty(ticketBean.getStartCity(),"");
        zpSDK.zp_draw_text_ex((width*0.4-left.length()*5)/2,total+6,left,"黑体",5,0,true,false,false);
        String right = isEmpty(ticketBean.getEndCity(),"");
        zpSDK.zp_draw_text_ex((width*0.4-right.length()*5)/2+width*0.6,total+6,right,"黑体",5,0,true,false,false);
        zpSDK.zp_draw_bitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.arrow),width*10*0.4-10,total*10-10);
        Log.d("ccccccc11111", "drawTicket2222222");
        total +=8;
        zpSDK.zp_draw_line(0,total,width,total,1);

        double lineHeight = 4;
        float lines = 1;

        zpSDK.zp_draw_text_ex(1,total+lineHeight*lines,"收货人：" +
                isEmpty(ticketBean.getUserName(),"")+" "+
                isEmpty(ticketBean.getUserPhone(),""),"黑体",2.8,0,false,false,false);

        lines++;
        zpSDK.zp_draw_text_ex(1,total+lineHeight*lines,"收货地址："+isEmpty(ticketBean.getUserAddress(),""),"黑体",2.8,0,false,false,false);

        String sendPerson = isEmpty(ticketBean.getSendPersonName(),"") + " " + isEmpty(ticketBean.getSendPersonPhone(),"");
        if (sendPerson.length() > 1){
            lines++;
            zpSDK.zp_draw_text_ex(1,total+lineHeight*lines,"发货人："+sendPerson,"黑体",2.8,0,false,false,false);
        }

        String sendPersonAddress = isEmpty(ticketBean.getSendPersonAddress(),"");
        if (sendPersonAddress.length() > 0){
            lines++;
            zpSDK.zp_draw_text_ex(1,total+lineHeight*lines,"发货地址："+sendPersonAddress,"黑体",2.8,0,false,false,false);
        }

        String deliveryPersonStr = isEmpty(ticketBean.getDeliveryPersonName(),"") + " " + isEmpty(ticketBean.getDeliveryPersonPhone(),"");
        if (deliveryPersonStr.length() > 1){
            lines++;
            zpSDK.zp_draw_text_ex(1,total+lineHeight*lines,"配送员："+deliveryPersonStr,"黑体",2.8,0,false,false,false);
        }
        Log.d("ccccccc11111", "drawTicket3333333333333");

        total += lines * lineHeight + 1.5;
        zpSDK.zp_draw_line(0,total,width,total,1);

        double keyPointY = total;


        zpSDK.zp_draw_text_ex(1,total+3.5,"品名："+isEmpty(ticketBean.getProductName(),""),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(1+width*0.333,total+3.5,"包装："+isEmpty(ticketBean.getPackageName(),""),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(1+width*0.667,total+3.5,"件数："+isEmpty(ticketBean.getNumber(),""),"黑体",2.8,0,false,false,false);

        zpSDK.zp_draw_text_ex(1,total+8.3,"重量："+isEmpty(ticketBean.getWeight(),"0")+"kg","黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(1+width*0.333,total+8.3,"保费："+ String.format("%.2f", Float.valueOf(isEmpty(ticketBean.getBaofeiFee(),"0"))),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(1+width*0.667,total+8.3,"运费："+ String.format("%.2f", Float.valueOf(isEmpty(ticketBean.getFreight(),"0"))),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_line(0,total+5,width,total+5,1);
        total +=10;
        zpSDK.zp_draw_line(0,total,width,total,1);
        Log.d("ccccccc11111", "drawTicket444444444444");
        //关键点
        double keyPointY1 = total;
        lineHeight = 5;
        lines = 1;
        List<String> strings = new ArrayList<>();
        strings.add(isEmpty(ticketBean.getVolume(),""));
        strings.add(isEmpty(ticketBean.getDeliveryFee(),""));
        strings.add(isEmpty(ticketBean.getLandFee(),""));
        strings.add(isEmpty(ticketBean.getZhuangxieFee(),""));
        strings.add(isEmpty(ticketBean.getTransFee(),""));
        strings.add(isEmpty(ticketBean.getOtherFee(),""));
        strings.add(isEmpty(ticketBean.getDaidianFee(),""));
        strings.add(isEmpty(ticketBean.getDaikuanFee(),""));
        strings.add(isEmpty(ticketBean.getDaishouFee(),""));
        strings.add(isEmpty(ticketBean.getBaoguanFee(),""));
        strings.add(isEmpty(ticketBean.getServiceFee(),""));
        strings.add(isEmpty(ticketBean.getPackageFee(),""));
        strings.add(isEmpty(ticketBean.getShengjiaFee(),""));
        strings.add(isEmpty(ticketBean.getShangloufee(), ""));
        Log.d("ccccccc11111", "drawTicket55555555555555");
        List<String> titles = new ArrayList<>();
        titles.add("体积：");
        titles.add("送货费：");
        titles.add("落地费：");
        titles.add("装卸费：");
        titles.add("垫中转费：");
        titles.add("其他费用：");
        titles.add("代垫货款：");
        titles.add("贷款手续费：");
        titles.add("代收货款：");
        titles.add("报关费：");
        titles.add("服务费：");
        titles.add("包装费：");
        titles.add("声价：");
        titles.add("上楼费：");
        Log.d("ccccccc11111数组", "titles::"+titles.toString());
        Log.d("ccccccc11111数组", "strings::"+strings.toString());
        Log.d("ccccccc11111", "drawTicket666666");
        int flag = 0;
        for (int i = 0; i < strings.size(); i++) {
            Log.d("ccccccc11111", "drawTicket7777777");
            if (strings.get(i).length() > 0){
                Log.d("ccccccc11111", "drawTicket888888888"+ String.valueOf((flag+1)%2 == 0));
                if ((flag+1)%2 == 0){ //抬头
                    Log.d("ccccccc11111", "drawTicket99999999");

                    String str = "";
                    /*if (titles.get(i).equals("体积：")){
                        str = titles.get(i)+ String.format("%d",Integer.valueOf(strings.get(i)));
                    }else {*/
                        str = titles.get(i)+ String.format("%.2f", Float.valueOf(strings.get(i)));
//                    }
                    zpSDK.zp_draw_text_ex(width*0.5+1,total+lineHeight*lines-1.3,str,"黑体",2.8,0,false,false,false);
                    zpSDK.zp_draw_line(0.1,total+lineHeight*lines,width,total+lineHeight*lines,1);
                    lines++;
                }else {
                    Log.d("ccccccc11111", "drawTicket1010101010");
                    String str = "";
                    /*if (titles.get(i).equals("体积：")){
                        str = titles.get(i)+ String.format("%d",Integer.valueOf(strings.get(i)));
                    }else {*/
                        str = titles.get(i)+ String.format("%.2f", Float.valueOf(strings.get(i)));
//                    }
                    zpSDK.zp_draw_text_ex(1,total+lineHeight*lines-1.3,str,"黑体",2.8,0,false,false,false);
                }
                flag++;
            }
        }
        if (flag%2==1){
            zpSDK.zp_draw_line(0.1,total+lineHeight*lines,width,total+lineHeight*lines,1);
            lines++;
        }

        zpSDK.zp_draw_text_ex(1,total+lineHeight*lines-1.3,"回单付："+(ticketBean.isHuidan() ? "是" : "否"),"黑体",2.8,0,false,false,false);

        total+=lineHeight*lines;
        zpSDK.zp_draw_line(width*0.5,keyPointY1,width*0.5,total-lineHeight,1);
        zpSDK.zp_draw_line(0,total,width,total,1);
        //画竖线

        zpSDK.zp_draw_text_ex(1,total+5.5,"合计费用："+ String.format("%.2f", Float.valueOf(isEmpty(ticketBean.getTotalFee(),"0"))),"黑体",3.5,0,true,false,false);
        zpSDK.zp_draw_text_ex(width*0.5+1,total+5.5, MoneyUtils.toChinese(isEmpty(ticketBean.getTotalFee(),"0")),"黑体",3.5,0,true,false,false);
        zpSDK.zp_draw_line(width*0.5,total,width*0.5,total+8,1);

        total +=8;

        zpSDK.zp_draw_line(0.1,keyPointY,0.1,total,1);
        zpSDK.zp_draw_line(width-0.1,keyPointY,width-0.1,total,1);

        zpSDK.zp_draw_line(0,total,width,total,1);
        zpSDK.zp_draw_text_ex(1,total+5,"备注:","黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(8,total+5,isEmpty(ticketBean.getRemarks(),""),"黑体",2.8,0,false,false,false);

        total +=8;
        zpSDK.zp_draw_line(0,total,width,total,1);

        zpSDK.zp_draw_text_box(1,total+2.7,width*0.667,width*0.337+2,"运输条款：" +
                "\n① 发货人需要注明货物名称，虚报品名的承运方一律不予保障。" +
                "\n② 发货人需要主动购买保险，未购买保险运费三倍给予赔偿，最高不过3000/票。" +
                "\n③ 当票货一个月内无人认领的，承运无保管责任，均作无效处理" +
                "\n④ 托运危险品和国家违禁物品由发货人承担一切责任","黑体",2.2,0,false,false,false);
        zpSDK.zp_draw_barcode2d(width*0.667+2,total+3,"http://weixin.qq.com/r/2zmvtyTEW3kkrWDU92zJ",
                zpSDK.BARCODE2D_TYPE.BARCODE2D_QRCODE,0,5,0);
        total += (3+width*0.337+2+2);
        zpSDK.zp_draw_text_ex(width*0.667+2,total-3,isEmpty(ticketBean.getTime(),""),"黑体",2.8,0,false,false,false);
        Log.e("total", String.valueOf(total));
        Log.d("ccccccc11111", "drawTicketBBBBBBBBB");
    }



    private void drawLabel(PrintLabelBean labelBean,int count) {
        zpSDK.zp_page_clear();
        double width = 68;
        double total = 0; //51.8  72
        zpSDK.zp_draw_line(0, total, width, total, 2);
        zpSDK.zp_draw_text_ex(1,total+6,isEmpty(labelBean.getGroupName(),"云企集团"),"黑体",4.8,0,true,false,false);
        zpSDK.zp_draw_text_box(21.5, total+6, width*0.667-22.1, 8, isEmpty(labelBean.getCompanyName(),""), "黑体", 2.5,0, false, false, false);

        zpSDK.zp_draw_line(width*0.667,total,width*0.667,total+8,2);
        zpSDK.zp_draw_text_ex(width*0.667+1,total+3.5,"运单号","黑体",2.5,0,false,false,false);
        zpSDK.zp_draw_text_ex(width*0.667+2.5,total+3.5+3.5,isEmpty(labelBean.getTrackNumber(),"")+"-"+ String.format("%02d",count),"黑体",2.8,0,false,false,false);

        total += 8;
        zpSDK.zp_draw_line(0,total,width,total,2);

        String startCity = isEmpty(labelBean.getStartCity(),"");

        zpSDK.zp_draw_text_ex(0.5,total+6,startCity,"黑体",4.5,0,false,false,false);
        zpSDK.zp_draw_line(startCity.length() * 4.5 + 2,4+total,startCity.length() * 4.5 + 2 + 6,4+total,4);
        zpSDK.zp_draw_text_ex(startCity.length()*4.5 + 2 + 6 + 2,total+6.3,isEmpty(labelBean.getEndCity(),""),"黑体",4.5,0,true,false,false);

        zpSDK.zp_draw_line(width * 0.75,total,width * 0.75,total+8,2);
        zpSDK.zp_draw_text_ex(width*0.75+0.5,total+2.8,"中转站","黑体",2.5,0,false,false,false);
        zpSDK.zp_draw_text_ex(width*0.75+0.5+1,total+7,isEmpty(labelBean.getTransStation(),""),"黑体",3,0,true,false,false);

        total+=8;
        zpSDK.zp_draw_line(0,total,width,total,2);

        zpSDK.zp_draw_line(width*0.5,total,width*0.5,total+9,2);
        zpSDK.zp_draw_line(width*0.5+width*0.5*0.4,total,width*0.5+width*0.5*0.4,total+9,2);

        zpSDK.zp_draw_text_ex(0.5,total+3.5,"收货人","黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(1.5,total+7.5,isEmpty(labelBean.getUserName(),""),"黑体",3.5,0,false,false,false);

        zpSDK.zp_draw_text_ex(width*0.5+0.5,total+3.5,"配送方式","黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(width*0.5+1.5,total+7.5,isEmpty(labelBean.getSendMethod(),""),"黑体",3,0,false,false,false);

        zpSDK.zp_draw_text_ex(width*0.5+width*0.5*0.4+0.5,total+3.5,"件数："+ String.format("%d/%s",count,isEmpty(labelBean.getNumber(),"1")),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_text_ex(width*0.5+width*0.5*0.4+0.5,total+7.5,"品名："+isEmpty(labelBean.getProductName(),""),"黑体",2.8,0,false,false,false);

        total+=9;
        zpSDK.zp_draw_line(0,total,width,total,2);

        zpSDK.zp_draw_text_ex(width * 0.25,total+3.5,isEmpty(labelBean.getSendPersonName(),"")+" "+isEmpty(labelBean.getTime(),""),"黑体",2.8,0,false,false,false);
        zpSDK.zp_draw_barcode(10,total+5,isEmpty(labelBean.getTrackNumber(),"")+"-"+ String.format("%02d",count), zpSDK.BARCODE_TYPE.BARCODE_CODE128,9,3,0);

        total+=16;
        zpSDK.zp_draw_line(0,total,width,total,2);
        zpSDK.zp_draw_line(0.1,0,0.1,total,2);
        zpSDK.zp_draw_line(width-0.1,0,width-0.1,total,2);
        Log.e("total", String.valueOf(total));
    }

    private String isEmpty(String value, String defaultValue){
        if (value == null || value.length() == 0){
            return defaultValue;
        }
        return value;
    }
    private double otherHeight(PrintTicketBean bean){
        double lineHeight = 5;
        String[] strings = {
                isEmpty(bean.getVolume(),""),
                isEmpty(bean.getShengjiaFee(),""),
                isEmpty(bean.getDeliveryFee(),""),
                isEmpty(bean.getLandFee(),""),
                isEmpty(bean.getZhuangxieFee(),""),
                isEmpty(bean.getTransFee(),""),
                isEmpty(bean.getOtherFee(),""),
                isEmpty(bean.getDaidianFee(),""),
                isEmpty(bean.getDaikuanFee(),""),
                isEmpty(bean.getDaishouFee(),""),
                isEmpty(bean.getBaoguanFee(),""),
                isEmpty(bean.getServiceFee(),""),
                isEmpty(bean.getPackageFee(),"")
        };
        int flag = 0;
        for (String string : strings) {
            if (string.length() > 0){
                flag++;
            }
        }

        int subLines = (strings.length/2+strings.length%2) - (flag/2+flag%2);

        String sendPerson = isEmpty(bean.getSendPersonName(),"") + " " + isEmpty(bean.getSendPersonPhone(),"");
        if (sendPerson.length() <=1){
            subLines++;
        }

        String sendPersonAddress = isEmpty(bean.getSendPersonAddress(),"");
        if (sendPersonAddress.length() == 0){
            subLines++;
        }

        String deliveryPersonStr = isEmpty(bean.getDeliveryPersonName(),"") + " " + isEmpty(bean.getDeliveryPersonPhone(),"");
        if (deliveryPersonStr.length() <= 1){
            subLines++;
        }

        return subLines*lineHeight;

    }






}
